package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.*;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sjsu.cloudmanager.client.CloudManager;
import com.sjsu.cloudmanager.client.LabManagerEvents;
import com.sjsu.cloudmanager.client.service.CloudLabServiceAsync;
import com.sjsu.cloudmanager.client.ui.model.CloudLabBean;

import java.util.List;

/**
 * Lab Form panel
 *
 * User: ckempaiah
 * Date: 10/3/11
 * Time: 9:42 AM
 * To change this template use File | Settings | File Templates.
 */
public class LabFormPanel extends LayoutContainer {

    FormBinding formBinding;
    FormPanel formPanel;

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        FormPanel panel = new FormPanel();
        panel.setHeaderVisible(false);

        TextField<String> name = new TextField<String>();
        name.setName("labName");
        name.setAllowBlank(false);
        name.setFieldLabel("Lab Name");
        panel.add(name);

        HiddenField<Integer> labId = new HiddenField<Integer>();
        labId.setName("labId");
        panel.add(labId);
        Button save = new Button("Save");

        Button cancel = new Button("Cancel");
        save.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent buttonEvent) {
                if (formPanel.isValid()) {

                    final CloudLabServiceAsync service = (CloudLabServiceAsync) Registry.get(CloudManager.CLOUD_LAB_SERVICE);
                    service.saveCloudLab((CloudLabBean)formBinding.getModel(), new AsyncCallback<Void>() {
                        @Override
                        public void onFailure(Throwable throwable) {
                            MessageBox.alert(MessageBox.ERROR, "Failed To Save ", null);
                        }

                        @Override
                        public void onSuccess(Void aVoid) {
                            MessageBox.alert(MessageBox.INFO, "Successfully Saved", null);
                            ToolbarAction.getInstance().fireEvent(LabManagerEvents.SaveLab, new BaseEvent(LabManagerEvents.SaveLab));

                        }
                    });
//                    RpcProxy<Void> proxy = new RpcProxy<Void>() {
//                        @Override
//                        public void load(Object loadConfig,
//                                         AsyncCallback<Void> callback) {
//
//                            service.saveCloudLab((CloudLabBean) formPanel.getModel(), callback);
//                        }
//                    };
                    //formPanel.submit();
                }
            }
        });
        panel.addButton(save);
        panel.addButton(cancel);

        FormButtonBinding fbb = new FormButtonBinding(panel);
        fbb.addButton(save);
        panel.setButtonAlign(Style.HorizontalAlignment.CENTER);
        formPanel = panel;
        save.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent buttonEvent) {
                if (formPanel.isValid()) {

                    formPanel.submit();
                }
            }
        });
        formBinding = new FormBinding(panel, true);


//        TextField<String> symbol = new TextField<String>();
//        symbol.setName("symbol");
//        symbol.setFieldLabel("Symbol");
//        panel.add(symbol);
//
//        NumberField open = new NumberField();
//        open.setName("last");
//        open.setFieldLabel("Last");
//        panel.add(open);
//
//        NumberField change = new NumberField();
//        change.setName("change");
//        change.setFieldLabel("Change");
//        change.setFormat(NumberFormat.getDecimalFormat());
//        panel.add(change);
//
//        DateField last = new DateField();
//        last.setName("date");
//        last.setFieldLabel("Updated");
//        panel.add(last);

        add(panel, new FitData());
    }

    public FormBinding getFormBinding() {
        return formBinding;
    }

    public FormPanel getFormPanel() {
        return formPanel;
    }


}
